const bodyParser = require('body-parser')
const Mock = require('mockjs')
const data = Mock.mock({
	'list|30': [{
		'title': '@ctitle',
		'price|10-2000': 1,
		"desc": '@cword(10,20)',
		"detail": '@cword(300,500)',
		"key": '@id',
	}]
})

let code = Math.random().toString().substr(2, 6)
module.exports = app => {
	app.use(bodyParser.json())

	app.post('/api/tel', (req, res) => {
		const { tel } = req.body;
		code = Math.random().toString().substr(2, 6)
		res.send({
			code: 200,
			data: code
		})
	})


	app.post('/api/tel/login', (req, res) => {
		const { userCode } = req.body;


		if (userCode === code) {
			res.send({
				code: 200,
				data: null,
				mess: '登录成功'
			})
		} else {
			res.send({
				code: 404,
				data: null,
				mess: '登录失败'
			})
		}

	})

	app.post('/api/list', (req, res) => {
		res.send({
			code: 200,
			data: data.list
		})
	})


	app.post('/api/detail', (req, res) => {
		const { key } = req.body
		const item = data.list.find(v => v.key === key)
		res.send({
			code: 200,
			data: item
		})
	})

	app.post('/api/list/add', (req ,res) => {
		data.list.unshift(req.body)
		res.send({
			code: 200,
			mess: '添加成功！！！'
		})
	})
}